package day1219;


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server1 {

    public static void main(String[] args) {
        Server1  ser=new Server1();//1.创建服务器端对象
        ser.start();//2.用服务器端对象调用start()
    }

    Socket s=null;
    public void start(){
        try {
            System.out.println("服务器等待客户端连接...");
            s=ss.accept();//2.接收客户端的连接
            System.out.println(s.getInetAddress()+"连接成功了");
            //3.从socket中获取输入流
            InputStream  in=s.getInputStream();//用Socket获取输入流
            InputStreamReader isr=new InputStreamReader(in);//将输入流进行封装
            BufferedReader  br=new BufferedReader(isr);

            //输出流(客户端返回数据)
            OutputStream out=s.getOutputStream();//字节输出流
            OutputStreamWriter  writer=new OutputStreamWriter(out);//字符输出流
            PrintWriter  pw=new PrintWriter(writer);

            String str=null;//4.用输入流读取数据
            while( (str=br.readLine())!=null  ){//5.打印读取的数据
                System.out.println(s.getInetAddress() +"说：" +str);
                pw.println(str);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }





    ServerSocket   ss=null;//1.用来充当服务器端
    Server1(){
        System.out.println("正在启动服务器...");
        try {
            ss=new ServerSocket(8088);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("启动服务器成功...");
    }
}
